/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-08-18
 * Time: 23:37
 */
//https://leetcode.cn/problems/minimum-window-substring/submissions/654321397/
public class Test {
    public static String minWindow(String s, String t) {
        if(s.length()<t.length()){
            return "";
        }
        int[] hash=new int[58];
        int n=0;
        for(int i=0;i<t.length();i++){
            hash[t.charAt(i)-'A']++;
            if(hash[t.charAt(i)-'A']==1){
                n++;
            }
        }
        int slen=s.length();
        int min=slen+1;
        int p=0;
        for(int p1=0,p2=0;p2<slen;p2++){
            int x=s.charAt(p2)-'A';
            hash[x]--;
            if(hash[x]==0){
                n--;
                if(n==0) {
                    while(n==0) {
                        hash[s.charAt(p1) - 'A']++;
                        if (hash[s.charAt(p1) - 'A'] == 1) {
                            n++;
                        }
                        p1++;
                    }
                    if (min > p2 - p1 + 2) {
                        min = p2 - p1 + 2;
                        p = p1-1;
                    }
                }
            }
        }
        return s.substring(p,p+min);
    }
    public static void main(String[] args) {
        String s="ADOBECODEBANC";
        String t="ABC";
        System.out.println(minWindow(s,t));
    }
}
